package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.LmTicketCatalogBig;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.LtCatalogDetailQueryObject;
import cn.wolfcode.trip.base.service.ILmTicketCatalogBigService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@org.springframework.stereotype.Controller
@RequestMapping("specialSubCommend")
public class SpecialSubCommendController {
   @Autowired
   private ILmTicketCatalogBigService lmTicketCatalogBigService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") LtCatalogDetailQueryObject qo, Model model){
        qo.setState(2);
        model.addAttribute("result",lmTicketCatalogBigService.getSpecialCommendByState(qo));
        model.addAttribute("bigs",lmTicketCatalogBigService.getLmTicketCatalogBigByState(1));
        return "specialSubCommend/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(LmTicketCatalogBig lmTicketCatalogBig, MultipartFile file){
        JSONResult result=new JSONResult();
        String imgUrl="";
        try {
            if(file != null && file.getSize() > 0){
                // 将图片上传到图片服务器
                imgUrl = UploadUtil.upload(file, UploadUtil.BASE_PATH + "upload");
            }
            if(file == null){
                lmTicketCatalogBigService.saveOrUpdate(lmTicketCatalogBig,lmTicketCatalogBig.getDetail().getCoverUrl());
                return  result;
            }
            lmTicketCatalogBigService.saveOrUpdate(lmTicketCatalogBig,imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
